﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace mqjlink
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void LogComDelegate(StringBuilder str); 
    public static class JlinkAPI
    {
        [DllImport("JlinkARM.dll")]
        public extern static int JLINKARM_Open();//打开Jlink
        [DllImport("JlinkARM.dll")]
        public extern static bool JLINKARM_IsOpen();//判断Jlink是否已经打开
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_TIF_Select(int param1);//选择接口类型（0--JTAG,1--SWD）
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_SetSpeed(int param1);//设置Jlink速度
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_Reset();//重置
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_Halt();//挂起
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static bool JLINKARM_IsHalted();
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_Go();//继续运行
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_Close();//关闭Jlink
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_IsConnected();//判断Jlink是否连接
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_TIF_GetAvailable();//获取可用的接口？
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_ReadReg(int regIndex);//读寄存器（1-14对应R1-R14）
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINK_GetSN();//返回Int型，数值大小就是序列号
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINK_GetDLLVersion();//返回Int型，数值大小就是Dll版本号
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINK_EMU_GetNumDevices();//返回Int型，数值大小是设备数量
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_GetDeviceId();//返回Int型，返回0？？？
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_GetId();//返回Int型，返回设备ID需要转成16进制
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_GetFirmwareString(IntPtr aa);//？？？未知
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static int JLINKARM_GetFeatureString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer);//返回字符串字节数组
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static StringBuilder JLINKARM_GetCompileDateTime();//获取Jlink固件编译时间
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_EnableLogCom(LogComDelegate aa);//JLINKARM_EnableLogCom(void (*DebugFunc)(const char *));//c++中函数指针对应委托，char *对应stringbuilder
        [DllImport("JlinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_ReadMemU32(UInt32 addr, UInt32 leng, ref UInt32 buf, ref byte status);//读取32位数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_WriteU32(UInt32 addr, UInt32 dat);//写入32位数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_ReadMemU16(UInt32 addr, UInt32 leng, ref UInt16 buf, ref byte status);//读取16位数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_WriteU16(UInt32 addr, UInt16 dat);//写入16位数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_ReadMemU8(UInt32 addr, UInt32 leng, ref byte buf, ref byte status);//读取8位数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_WriteU8(UInt32 addr, byte dat);//写入8位数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_WriteMem(UInt32 addr, UInt32 size, byte[] buf);//写入一段数据
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_BeginDownload(UInt32 Flag);//开始下载，flag为0，具体含义不知
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_EndDownload();//结束下载
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static void JLINKARM_ExecCommand(string cmd,int v1,int v2,int v3);//具体参数不知，可以运行
    }
}
